﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ZhaoLuJiHuaWindow : ChildWindow
    {
        private ZhaoLuJiHuaModel ZhaoLuJiHua = new ZhaoLuJiHuaModel();
        private ObservableCollection<ZhaoLuJiHuaDetailModel> zhaoLuJiHuaDetail = new ObservableCollection<ZhaoLuJiHuaDetailModel>();

        private ObservableCollection<ZhaoLuJiHuaDetail1Model> zhaoLuJiHuaDetail1 =
            new ObservableCollection<ZhaoLuJiHuaDetail1Model>();
        private ZhaoLuService.ZhaoLuServiceClient Client = new ZhaoLuServiceClient();
        public int ID;
        public int IDXuNi;
        public int Year;
        public string Title;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iDXuNi">当其为0，则为新增</param>
        /// <param name="iD"></param>
        /// <param name="year"></param>
        /// <param name="title"></param>
        /// <param name="chakan"></param>
        public ZhaoLuJiHuaWindow(int iDXuNi,int iD, int year,string title,bool chakan)
        {
            InitializeComponent();
            ID = iD;
            Year = year;
            Title = title;
            te.Text = Title;
            IDXuNi = iDXuNi;
            ZhaoLuJiHua.ID = ID;
            ZhaoLuJiHua.Year = Year;
            ZhaoLuJiHua.Name = Title;
            Client.GetExcelFileCompleted += new EventHandler<GetExcelFileCompletedEventArgs>(Client_GetExcelFileCompleted);
            if(chakan == true)
            {
                OKButton.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                OKButton.Visibility = System.Windows.Visibility.Visible;
            }
            Load();
        }

        void Client_GetExcelFileCompleted(object sender, GetExcelFileCompletedEventArgs e)
        {
            List<byte> content = e.UserState as List<byte>;
            FileTransferStream fff = e.Result;
            if (fff.IsFinished == false)
            {
                content.AddRange(fff.FileContent);
                Client.GetExcelFileAsync(zhaoLuJiHuaDetail[0], zhaoLuJiHuaDetail1, Session.LoginUser.LoginName, content);
            }
            else
            {
                content.AddRange(fff.FileContent);
                byte[] bytes = content.ToArray();
                ExportPersonnelInfo.Write(bytes, 0, bytes.Length);
                ExportPersonnelInfo.Close();
                ExportPersonnelInfo.Dispose();
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功!");
            }
        }
        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            //try
            //{
            //    double.Parse(((TextBox) sender).Text);
            //}
            //catch(Exception ex)
            //{
            //    MessageBox.Show(ex.Message.ToString());
            //}
            Match m = Regex.Match(((TextBox)sender).Text, pattern);   // 匹配正则表达式
            if (!string.IsNullOrEmpty(((TextBox)sender).Text) && ((TextBox)sender).Text != "0")
                if (!m.Success)   // 输入的不是数字
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                    ((TextBox)sender).Text = "";
                    //((TextBox)sender).Text = param1;   // textBox内容不变

                    // 将光标定位到文本框的最后
                    //((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
                }

        }
        public void Load()
        {
            if(IDXuNi == 0)
            {
                Client.GetZhaoLuJiHuaDetailModelCompleted -= new EventHandler<GetZhaoLuJiHuaDetailModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetailModelCompleted);
                Client.GetZhaoLuJiHuaDetailModelCompleted += new EventHandler<GetZhaoLuJiHuaDetailModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetailModelCompleted);
                Client.GetZhaoLuJiHuaDetailModelAsync(0, Year);
                Client.GetZhaoLuJiHuaDetail1ModelCompleted -= new EventHandler<GetZhaoLuJiHuaDetail1ModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetail1ModelCompleted);
                Client.GetZhaoLuJiHuaDetail1ModelCompleted += new EventHandler<GetZhaoLuJiHuaDetail1ModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetail1ModelCompleted);
                Client.GetZhaoLuJiHuaDetail1ModelAsync(0, Year);
            }
            else
            {
                Client.GetZhaoLuJiHuaDetailModelCompleted -= new EventHandler<GetZhaoLuJiHuaDetailModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetailModelCompleted);
                Client.GetZhaoLuJiHuaDetailModelCompleted += new EventHandler<GetZhaoLuJiHuaDetailModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetailModelCompleted);
                Client.GetZhaoLuJiHuaDetailModelAsync(ID, Year);
                Client.GetZhaoLuJiHuaDetail1ModelCompleted -= new EventHandler<GetZhaoLuJiHuaDetail1ModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetail1ModelCompleted);
                Client.GetZhaoLuJiHuaDetail1ModelCompleted += new EventHandler<GetZhaoLuJiHuaDetail1ModelCompletedEventArgs>(Client_GetZhaoLuJiHuaDetail1ModelCompleted);
                Client.GetZhaoLuJiHuaDetail1ModelAsync(ID, Year);
            }
        }

        void Client_GetZhaoLuJiHuaDetail1ModelCompleted(object sender, GetZhaoLuJiHuaDetail1ModelCompletedEventArgs e)
        {
            ZhaoLuJiHuaDetail1.ItemsSource = e.Result;
        }

        void Client_GetZhaoLuJiHuaDetailModelCompleted(object sender, GetZhaoLuJiHuaDetailModelCompletedEventArgs e)
        {
             ZhaoLuJiHuaDetail.ItemsSource = e.Result;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            zhaoLuJiHuaDetail = (ObservableCollection<ZhaoLuJiHuaDetailModel>) ZhaoLuJiHuaDetail.ItemsSource;
            zhaoLuJiHuaDetail1 = (ObservableCollection<ZhaoLuJiHuaDetail1Model>)ZhaoLuJiHuaDetail1.ItemsSource;
            Client.InsertZhaoLuJiHuaCompleted -= new EventHandler<InsertZhaoLuJiHuaCompletedEventArgs>(Client_InsertZhaoLuJiHuaCompleted);
            Client.InsertZhaoLuJiHuaCompleted += new EventHandler<InsertZhaoLuJiHuaCompletedEventArgs>(Client_InsertZhaoLuJiHuaCompleted);
            Client.InsertZhaoLuJiHuaAsync(ID, zhaoLuJiHuaDetail[0], zhaoLuJiHuaDetail1);
        }

        void Client_InsertZhaoLuJiHuaCompleted(object sender, InsertZhaoLuJiHuaCompletedEventArgs e)
        {
            if (e.Result == true && ID == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
            if (e.Result == true && ID != 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }
        private Stream ExportPersonnelInfo = null;
        private void DaoChuData_Click(object sender, RoutedEventArgs e)
        {
            zhaoLuJiHuaDetail = (ObservableCollection<ZhaoLuJiHuaDetailModel>)ZhaoLuJiHuaDetail.ItemsSource;
            zhaoLuJiHuaDetail1 = (ObservableCollection<ZhaoLuJiHuaDetail1Model>)ZhaoLuJiHuaDetail1.ItemsSource;
            if (zhaoLuJiHuaDetail != null && zhaoLuJiHuaDetail1 != null && zhaoLuJiHuaDetail.Count > 0 && zhaoLuJiHuaDetail1.Count > 0)
            {
                var downLoad = new SaveFileDialog()
                {
                    //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls", //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                    Filter = "Excel files(2003) (*.xls)|*.xls",
                };
                if (downLoad.ShowDialog() == true)
                {
                    ExportPersonnelInfo = downLoad.OpenFile();
                    List<byte> content = new List<byte>();
                    Client.GetExcelFileAsync(zhaoLuJiHuaDetail[0], zhaoLuJiHuaDetail1, Session.LoginUser.LoginName,
                                             content);
                }
            }
        }
    }
}
